//
//  ListOfVViewController.m
//  NewMyMusicAPP
//
//  Created by 11_wyq on 15/6/25.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "ListOfVViewController.h"

@interface ListOfVViewController ()

@property (retain ,nonatomic) MBProgressHUD *MBP;



@end

@implementation ListOfVViewController



-(void)viewWillAppear:(BOOL)animated
{
    
    
    
    
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;//取消navigationController透明度
    
    //       给导航栏上放自定义label
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100 * WIDTH, 30 * HEIGHT)];
    [view setBackgroundColor:[UIColor clearColor]];
    self.navigationItem.titleView = view;
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(20 * WIDTH, 0, 100 * WIDTH, 30 * HEIGHT)];
    title.text = @"排行榜";
    title.textColor = [UIColor whiteColor];
    [title setFont:[UIFont fontWithName:@"Helvetica-Bold" size:20]];//加粗字体
    [view addSubview:title];
    
    
    
       self.view.backgroundColor = [UIColor whiteColor];
    UIImage *image2 = [UIImage imageNamed:@"menu.png"];
    image2 = [image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:image2 style:UIBarButtonItemStylePlain target:self action:@selector(locker)];
    

    
    self.numberArray = [NSMutableArray array];
    _numberArray = [NSMutableArray arrayWithObjects:@"内地", @"韩国",@"港台",@"日本",@"欧美",nil];

    
    
    UIView *buttonView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MYWIDTH, 56 * HEIGHT)];
    [self.view addSubview:buttonView];
    buttonView.backgroundColor = [UIColor blackColor];
    
    for (int i = 0; i < 5; i++) {
        NSString *string = [_numberArray objectAtIndex:i];
        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((25 + (i * 50 ) + (i * 20)) * WIDTH, 0, 50 *WIDTH, 40 * HEIGHT);
        [buttonView addSubview:button];
        button.tag = 1000 + i;
        [button setFont:[UIFont fontWithName:@"Helvetica-Bold" size:17]];//加粗字体
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitle:string forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    
    
    
        
    
    
    
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 40, MYWIDTH, 600 * HEIGHT)];
    [self.view addSubview:_myTableView];
    _myTableView.dataSource = self;
    _myTableView.delegate = self;
    _myTableView.backgroundColor = [UIColor blackColor];
     self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;//取消cell线
    
    
    
    self.array = [NSMutableArray array];
    
    _offset = 0;
    self.urlDic = [NSMutableDictionary dictionary];
    [_urlDic setObject:@"0" forKey:@"D-A"];
    [_urlDic setObject:@"true" forKey:@"date"];
    [_urlDic setObject:@"0" forKey:@"offset"];
    [_urlDic setObject:@"20" forKey:@"size"];
    [_urlDic setObject:@"ML" forKey:@"area"];
    
    [self getmassage];
    
    [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor colorWithHexString:@"FF9966"] forState:UIControlStateNormal];
 
    
    
    
    
    
    _number = 0;
    [self addFooter];//上拉加载更多
    [self addHeader];//下拉刷新
    
    
    //动态小黄人
    [GiFHUD setGifWithImageName:@"xiaohuangren.gif"];
    [GiFHUD show];
    
    
    
    
    
    
    
}




-(void)addHeader
{
    __block ListOfVViewController *listVC=self;
    
    
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getmassage];
        
        
        
    }];
    
    
}



#pragma mark ---button点击触发

-(void)buttonAction:(UIButton *)button
{
    
    _offset = 0;
    _number = 0;
    
    if (button == (UIButton *)[self.view viewWithTag:1000]) {
       
        self.array = [NSMutableArray array];
        [_urlDic setObject:@"ML" forKey:@"area"];
       [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       
    }
    if (button == (UIButton *)[self.view viewWithTag:1001]) {
    
     
        self.array = [NSMutableArray array];
        [_urlDic setObject:@"KR" forKey:@"area"];
        
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
   }
    if (button == (UIButton *)[self.view viewWithTag:1002]) {
    
        
        self.array = [NSMutableArray array];
        [_urlDic setObject:@"HT" forKey:@"area"];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
    if (button == (UIButton *)[self.view viewWithTag:1003]) {
        
        self.array = [NSMutableArray array];
        [_urlDic setObject:@"JP" forKey:@"area"];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1004] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        
    }
    if (button == (UIButton *)[self.view viewWithTag:1004]) {
        
        self.array = [NSMutableArray array];
        [_urlDic setObject:@"US" forKey:@"area"];
        
        [(UIButton *)[self.view viewWithTag:1001] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1002] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1003] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [(UIButton *)[self.view viewWithTag:1000] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
    }
    
    [button setTitleColor:[UIColor colorWithHexString:@"FF9966"] forState:UIControlStateNormal];
    [self.MBP show:YES];
     [self getmassage];
    

}



#pragma mark --- 上拉加载
-(void)addFooter
{
    __block ListOfVViewController *ListOfV=self;
    self.myTableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
      _offset = _offset + 20;//每次下拉+20
        
    
        if (_offset < 60) {
            
            
             NSString *page = [NSString stringWithFormat:@"%ld",_offset];
            [_urlDic setObject:page forKey:@"offset"];
            [self getmassage];
            return ;
        }
        UIAlertView *alertV = [[UIAlertView alloc]initWithTitle:@"提示" message:@"没有更多啦" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        [alertV show];
       
        
    }];
    

    
    
    
    
}


#pragma mark --- 抽屉
-(void)locker
{
    [GiFHUD dismiss];//动态图隐藏 
    [self.sideMenuViewController presentLeftMenuViewController];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.array.count != 0) {
        return self.array.count;

    }else
    {
        return 0;
    }
    
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellName = @"myCell";
    ListOfVCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [[ListOfVCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellName];
    }
    
    //渐变效果
    cell.layer.shadowColor = [[UIColor blackColor]CGColor];
    cell.alpha = 0.1;
    [UIView beginAnimations:@"rotation" context:NULL];
    [UIView setAnimationDuration:1.5];//时间
    cell.alpha = 1;
    [UIView commitAnimations];
    
    
    if (self.array.count != 0) {
        ListOfVModel *model = [self.array objectAtIndex:indexPath.row];
        cell.model = model;
        [[DataHandle shareHandleController]openDB];
        
        NSMutableArray *array = [[DataHandle shareHandleController]selectCollect];
        for (CollectModel *collectModel in array) {
            if ([collectModel.title isEqualToString:model.title]) {
                cell.isCollection = YES;
                cell.model = model;
                return cell;
            }
        }
        
        cell.isCollection = NO;
        cell.model = model;
    }
    
    
    
    
    
    
    
    return cell;
}

-(void)getmassage
{

    
   
    
    
    [WebRequest connectWithUrl:VBangLeft parmater:_urlDic requestHeader:RequestHeader httpMethod:@"GET" view:self.view block:^(id data) {
        
        if(data){
            
          
            NSMutableDictionary *dic = data;
            NSMutableArray *dicArray = [dic objectForKey:@"videos"];
            NSLog(@"dicArray = %ld",dicArray.count);
            for (NSMutableDictionary *smallDic  in dicArray) {
                _number ++;
                ListOfVModel *model = [[ListOfVModel alloc]init];
                model.myId = [smallDic objectForKey:@"id"];
                model.title = [smallDic objectForKey:@"title"];
                model.artistName = [smallDic objectForKey:@"artistName"];
                model.score = [smallDic objectForKey:@"score"];
                model.number = [NSString stringWithFormat:@"%ld",_number];
                model.albumImg = [smallDic objectForKey:@"albumImg"];
                [_array addObject:model];
               // NSLog(@"_array = %ld",_array.count);
                [model release];
               
                
            }
            [self.myTableView reloadData];
            [self.myTableView.header endRefreshing];//结束下拉刷新
            [self.myTableView.footer endRefreshing];//结束上拉加载
            [GiFHUD dismiss];//动态图隐藏
        }
        
        
    
        
        
        
    } refresh:^{
        
        [GiFHUD dismiss];//动态图隐藏
  
        //延迟8秒执行;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
          //  [self getmassage];
        });
    }];

}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if ([DataHandle shareHandleController].monitorNetwork == NO) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"您的网络不给力,请检查网络设置" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }

    ListOfVModel *model = [self.array objectAtIndex:indexPath.row];
    
    
    
    VideoPlayerViewController *playerVC = [[VideoPlayerViewController alloc]init];
    playerVC.Id = model.myId;
    playerVC.type = @"VIDEO";
    playerVC.hidesBottomBarWhenPushed = YES;//隐藏TabBar
    [self.navigationController pushViewController:playerVC animated:YES];
    
    
    
    
    
}




//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200 * HEIGHT;
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
